//
//  AppDelegate.m
//  B-72K
//
//  Created by sos on 16/3/7.
//  Copyright © 2016年 sos. All rights reserved.
//

#import "AppDelegate.h"
#import "NewsViewController.h"
#import "InvestViewController.h"
#import "VideoViewController.h"
#import "UserViewController.h"
#import <iflyMSC/iflyMSC.h>
#import "JPUSHService.h"
#import "AllPrefixHeader.pch"
#import "GuideViewController.h"
#import "DataBaseManager.h"

#import <TencentOpenAPI/TencentOAuth.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//    
//    NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"56e7bf6c"];
//    [IFlySpeechUtility createUtility:initString];
    
    // 打开数据库 创建表
    DataBaseManager *dbManager = [DataBaseManager shareInstance];
    [dbManager openDB];
    // 创建浏览数据的表
    [dbManager createBrowseDb];
    [dbManager createCollectDb];
    
    [ThemeManage shareThemeManage].isNight = [[NSUserDefaults standardUserDefaults] boolForKey:@"night"];

    
    //设置sdk的log等级，log保存在下面设置的工作路径中
    [IFlySetting setLogFile:LVL_ALL];
    
    //打开输出在console的log开关
    [IFlySetting showLogcat:YES];
    
    //设置sdk的工作路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [paths objectAtIndex:0];
    [IFlySetting setLogFilePath:cachePath];
    
    //创建语音配置,appid必须要传入，仅执行一次则可
    NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"56e7bf6c"];
    
    //所有服务启动前，需要确保执行createUtility
    [IFlySpeechUtility createUtility:initString];

    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    // Override point for customization after application launch.
    
    NewsViewController *news = [[NewsViewController alloc]init];
    UINavigationController *naviNews = [[UINavigationController alloc]initWithRootViewController:news];
    [naviNews.navigationBar NightWithType:UIViewColorType1];


    naviNews.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"新闻" image:[UIImage imageNamed:@"xinwen-2"] tag:1000];
     InvestViewController *invest = [[InvestViewController alloc]init];
    UINavigationController *naviInvest = [[UINavigationController alloc]initWithRootViewController:invest];
    [naviInvest.navigationBar NightWithType:UIViewColorType1];

    naviInvest.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"话题" image:[UIImage imageNamed:@"huati"] tag:1000];
    VideoViewController *video = [[VideoViewController alloc]init];
    UINavigationController *naviVideo = [[UINavigationController alloc]initWithRootViewController:video];
    naviVideo.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"视频" image:[UIImage imageNamed:@"shipin-2"] tag:3000];
    [naviVideo.navigationBar NightWithType:UIViewColorType1];


    
    UserViewController *user = [[UserViewController alloc]init];
    UINavigationController *naviUser = [[UINavigationController alloc]initWithRootViewController:user];
    naviUser.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[UIImage imageNamed:@"wode"] tag:4000];
    [naviUser.navigationBar NightWithType:UIViewColorType1];

    
    self.tabBar = [[UITabBarController alloc]init];
    _tabBar.viewControllers = @[naviNews,naviInvest,naviVideo,naviUser];
    [_tabBar.tabBar NightWithType:UIViewColorType2];
    self.window.rootViewController = _tabBar;
    
    
    // 引导页
    //    BOOL isFirst = [[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"];
    //    if (!isFirst) {
    //        [[NSUserDefaults standardUserDefaults ] setBool:YES forKey:@"firstLaunch"];
    
    
    //    }else{
    //    self.window.rootViewController = tabBar;
    //    }
    GuideViewController *userrVC = [[GuideViewController alloc] init];
    self.window.rootViewController = userrVC;

    
    // 极光推送
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        // 可以添加自定义categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
    }else{
        // categories 必须为nil
        
        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil];
        
    }
    // Required
    //如需兼容旧版本的方式，请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化和同时使用pushConfig.plist文件声明appKey等配置内容。
    [JPUSHService setupWithOption:launchOptions appKey:@"a7cde7b118d80e44aa389bfa" channel:nil apsForProduction:YES];
    

    
    return YES;
}
#pragma mark - 重写handleopenURL

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    // 微信
    return [TencentOAuth HandleOpenURL:url];
}
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    // 微信
    return [TencentOAuth HandleOpenURL:url];
}
// 设备注册远程通知
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    
    // Required
    [JPUSHService registerDeviceToken:deviceToken];
    NSLog(@"注册<<<<<%@",deviceToken);
}
// 用户收到远程通知
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    
    [JPUSHService handleRemoteNotification:userInfo];
    NSLog(@"用户收到>>>>%@",userInfo);
    
}
// 完成远程通知处理程序
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    
    // ios7 以后需要支持的
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
    
    
}
// 注册远程通知后失败结果
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    
    //Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error:%@",error);
    
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
